package com.hx.voice.recognition.common.rest;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hx.voice.recognition.common.context.BaseContextHandler;
import com.hx.voice.recognition.common.msg.ObjectRestResponse;
import com.hx.voice.recognition.common.msg.TableResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @descripe：
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/21 15:24
 * @version：V1.0
 */
public class BaseController<T> {

    @Autowired
    private IService<T> baseService;

    /**
     * @description 新增
     * @method
     * @param
     * @return
     * @date: 2019-06-12
     * @author:zhoujinbing
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<T> add(@RequestBody T T){
        baseService.saveOrUpdate(T);
        return new ObjectRestResponse<T>();
    }

    /**
     * @description 根据ID查询对象
     * @method
     * @param
     * @return
     * @date: 2019-06-12
     * @author:zhoujinbing
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<T> get(@PathVariable int id){
        ObjectRestResponse<T> entityObjectRestResponse = new ObjectRestResponse<>();
        T T =baseService.getById(id);
        entityObjectRestResponse.data(T);
        return entityObjectRestResponse;
    }

    /**
     * @description 修改
     * @method
     * @param
     * @return
     * @date: 2019-06-12
     * @author:zhoujinbing
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<T> update(@RequestBody T T){
        baseService.saveOrUpdate(T);
        return new ObjectRestResponse<T>();
    }

    /**
     * @description 删除
     * @method
     * @param
     * @return
     * @date: 2019-06-12
     * @author:zhoujinbing
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<T> remove(@PathVariable int id){
        baseService.removeById(id);
        return new ObjectRestResponse<T>();
    }

    /**
     * @description 查询所有
     * @method
     * @param
     * @return
     * @date: 2019-06-12
     * @author:zhoujinbing
     */
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
    public List<T> all(){
        return baseService.list();
    }

    /**
     * @description 分页查询数据
     * @method
     * @param
     * @return
     * @date: 2019-06-12
     * @author:zhoujinbing
     */
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<T> list(@RequestParam Map<String, Object> params){
        //当前页码
        int page = 1;
        //每页条数
        int limit = 10;

        if(params.get("page")!=null) {
            page = Integer.parseInt(params.get("page").toString());
        }
        if(params.get("limit")!=null) {
            limit = Integer.parseInt(params.get("limit").toString());
        }
        params.remove("page");
        params.remove("limit");

        Wrapper<T> TWrapper = new QueryWrapper<>();
        Set<Map.Entry<String, Object>> entries = params.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            ((QueryWrapper<T>) TWrapper).like(entry.getKey(),entry.getValue());
        }

        Page<T> pages = new Page<>(page,limit);
        IPage<T> pageData = baseService.page(pages,TWrapper);
        return new TableResultResponse<T>(pageData.getTotal(), pageData.getRecords());
    }

    /**
     * @description 查询当前用户名
     * @method
     * @param
     * @return
     * @date: 2019/6/11 10:09
     * @author:zhoujinbing
     */
    public String getCurrentUserName(){
        return BaseContextHandler.getUsername();
    }



}